package com.diu.domain.strategy.service.rule.filter.impl;

import com.diu.domain.strategy.model.entity.RuleActionEntity;
import com.diu.domain.strategy.model.entity.RuleMatterEntity;
import com.diu.domain.strategy.model.entity.StrategyRuleEntity;
import com.diu.domain.strategy.model.vo.RuleLogicCheckTypeVO;
import com.diu.domain.strategy.repository.IStrategyRepository;
import com.diu.domain.strategy.service.annotation.LogicStrategy;
import com.diu.domain.strategy.service.rule.filter.ILogicFilter;
import com.diu.domain.strategy.service.rule.filter.factory.DefaultLogicFactory;
import com.diu.types.common.Constants;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

/**
 * @author #diu
 * @description #
 * @create #2025/10/11 20:05
 */
@Component
@LogicStrategy(logicMode = DefaultLogicFactory.LogicModel.RULE_WHITELIST)
public class RuleWhiteListLogicFilter implements ILogicFilter<RuleActionEntity.RaffleBeforeEntity> {
    @Resource
    private IStrategyRepository repository;

    @Override
    public RuleActionEntity<RuleActionEntity.RaffleBeforeEntity> filter(RuleMatterEntity ruleMatterEntity) {
        // 1. 查询当前策略下的白名单规则
        StrategyRuleEntity strategyRuleEntity = repository.queryStrategyRuleByStrategyIdAndRuleModel(ruleMatterEntity.getStrategyId(), ruleMatterEntity.getRuleModel());
        if (null == strategyRuleEntity) {
            return RuleActionEntity.<RuleActionEntity.RaffleBeforeEntity>builder()
                    .code(RuleLogicCheckTypeVO.ALLOW.getCode())
                    .info(RuleLogicCheckTypeVO.ALLOW.getInfo())
                    .build();
        }
        String userId = ruleMatterEntity.getUserId();
        // 2. 查询用户是否在白名单中
        for(String ruleValueList: strategyRuleEntity.getRuleValue().split(Constants.SPACE)){
            for(String whiteUserId: ruleValueList.split(Constants.COLON)[1].split(Constants.SPLIT)){
                if(userId.equals(whiteUserId)){
                    return RuleActionEntity.<RuleActionEntity.RaffleBeforeEntity>builder()
                            .code(RuleLogicCheckTypeVO.TAKE_OVER.getCode())
                            .info(RuleLogicCheckTypeVO.TAKE_OVER.getInfo())
                            .ruleModel(strategyRuleEntity.getRuleModel())
                            .data(new RuleActionEntity.RaffleBeforeEntity().builder()
                                    .strategyId(ruleMatterEntity.getStrategyId())
                                    .awardId(ruleValueList.split(Constants.COLON)[0] == null ? null : Integer.parseInt(ruleValueList.split(Constants.COLON)[0]))
                                    .beforeState("白名单用户，直接奉上大奖")
                                    .build())
                            .build();
                }
            }
        }
        return RuleActionEntity.<RuleActionEntity.RaffleBeforeEntity>builder()
                .code(RuleLogicCheckTypeVO.ALLOW.getCode())
                .info(RuleLogicCheckTypeVO.ALLOW.getInfo())
                .build();
    }
}
